package com.zpphis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpphis.entity.Menu;
import com.zpphis.entity.UserMenu;
import com.zpphis.mapper.MenuMapper;
import com.zpphis.mapper.UserMenuMapper;
import com.zpphis.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zpp
 * @since 2022-11-22
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private UserMenuMapper userMenuMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Map<String, Object>> buildMenu(int userType) {
        List<Map<String, Object>> mlist = new ArrayList<>();
        LambdaQueryWrapper<Menu> mqw = new LambdaQueryWrapper<>();
        if (userType != 0) {
            // 根据user_type_id获取满足权限配置的menu_id
            LambdaQueryWrapper<UserMenu> umqw = new LambdaQueryWrapper<>();
            umqw.select(UserMenu::getMenuId);
            umqw.eq(UserMenu::getUserTypeId, userType);
            List<Object> ids = userMenuMapper.selectObjs(umqw);
            if (ids.size() > 0) {
                mqw.in(Menu::getId, ids);
            } else {
                return mlist;
            }
        }
        List<Menu> menus = menuMapper.selectList(mqw);

        for (Menu m0 : menus) {
            if (m0.getParentId() == 0) {
                Map<String, Object> mp0 = new HashMap<>();
                List<Map<String, Object>> m0child = new ArrayList<>();
                mp0.put("title", m0.getMenuTitle());
                mp0.put("icon", m0.getMenuIcon());
                mp0.put("href", m0.getMenuUrl());
                mp0.put("target", "_self");
                mp0.put("child", m0child);
                mlist.add(mp0);
                for (Menu m1 : menus) {
                    if (m1.getParentId() == m0.getId()) {
                        Map<String, Object> mp1 = new HashMap<>();
                        List<Map<String, Object>> m1child = new ArrayList<>();
                        mp1.put("title", m1.getMenuTitle());
                        mp1.put("icon", m1.getMenuIcon());
                        mp1.put("href", m1.getMenuUrl());
                        mp1.put("target", "_self");
                        mp1.put("child", m1child);
                        m0child.add(mp1);
                        for (Menu m2 : menus) {
                            if (m2.getParentId() == m1.getId()) {
                                Map<String, Object> mp2 = new HashMap<>();
                                mp2.put("title", m2.getMenuTitle());
                                mp2.put("icon", m2.getMenuIcon());
                                mp2.put("href", m2.getMenuUrl());
                                mp2.put("target", "_self");
                                m1child.add(mp2);
                            }
                        }
                    }
                }
            }
        }
        return mlist;
    }
}
